﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Drawing
{
    /// <summary>
    /// ImageExtension
    /// </summary>
    public static partial class ImageExtension
    {
        /// <summary>
        /// 剪切图片
        /// </summary>
        /// <param name="this"></param>
        /// <param name="width">目标图片的width</param>
        /// <param name="height">目标图片的height</param>
        /// <param name="x">开始剪切的x coordinate[左上角的 x 坐标]</param>
        /// <param name="y">开始剪切的x coordinate[左上角的 y 坐标]</param>
        /// <returns></returns>
        public static Image Cut(this Image @this, int width, int height, int x, int y)
        {
            var r = new Bitmap(width, height);
            var destinationRectange = new Rectangle(0, 0, width, height);
            var sourceRectangle = new Rectangle(x, y, width, height);
            using (Graphics g = Graphics.FromImage(r))
            {
                g.DrawImage(@this, destinationRectange, sourceRectangle, GraphicsUnit.Pixel);
            }
            return r;
        }
        /// <summary>
        /// 将图片按指定长度进行缩放
        /// </summary>
        /// <param name="this"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static Image Scale(this Image @this, int width, int height)
        {
            var r = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(r))
            {
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(@this, 0, 0, width, height);
            }
            return r;
        }
        /// <summary>
        /// 按一定比例进行绽放
        /// </summary>
        /// <param name="this"></param>
        /// <param name="ratio">绽放比例</param>
        /// <returns></returns>
        public static Image Scale(this Image @this, double ratio)
        {
            int width = Convert.ToInt32(@this.Width * ratio);
            int height = Convert.ToInt32(@this.Height * ratio);
            var r = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(r))
            {
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(@this, 0, 0, width, height);
            }
            return r;
        }
    }
}
